/*
  ==============================================================================

    ITunesLibrary.h
    Created: 6 Feb 2011 6:27:15pm
    Author:  David Rowland

  ==============================================================================
*/

#ifndef __ITUNESLIBRARY_H_CF4E7153__
#define __ITUNESLIBRARY_H_CF4E7153__

#include "../core/dRowAudio_StandardHeader.h"
#include "dRowAudio_ITunesLibaryParser.h"

class ITunesLibrary : public Timer,
					  public DeletedAtShutdown
{
public:
	
	juce_DeclareSingleton(ITunesLibrary, false);
	
	ITunesLibrary();
	
	~ITunesLibrary();
	
	/**	Sets a new library to base the ValueTree on.
		This will delete any old tree data so either take a copy of it or be
		prepared to lose it.
	 */
	void setLibraryFile(File newFile);

    /** Attemps to load a saved library file.
        This will return true if a saved library file has been loaded.
        If the file does not contain a valid ITunesLibrary database the default
        system library will loaded and the method will return false.
        
        @returns true if the saved library was loaded successfully, false if
        not and the default system library was used.
     */
    bool loadSavedLibraryIfNewer(File savedLibraryFile);

    /** Returns the file that this library was generated from.
     */
    const File& getLibraryFile();

    /** Returns the ValueTree that is being filled.
     */
	ValueTree getLibraryTree()	{	return libraryTree;	}

    /** This can be used to load a previously saved iTuneLibarary;
        This has to be in the same format as that generated by this class.
     */
    void setLibraryTree(ValueTree newTreeToUse);

    /** @internal */
	void timerCallback();

	//==============================================================================
    /** This returns the default iTunes library file.
        E.g. on Mac this will be something like:
        "/Users/yourname/Music/iTunes/iTunes Music Library.xml"
     */
	static File getDefaultITunesLibraryFile();
    
	//==============================================================================
    /** A class for receiving callbacks from an ITunesLibrary.
	 
		This will repeatedly call libraryUpdated so you can respond to any changes
		that may have happened until a single call to libraryFinished() where you
		may want to do some additional set-up.
	 
		@see ITunesLibrary::addListener, ITunesLibrary::removeListener
	 */
    class  Listener
    {
    public:
        //==============================================================================
        /** Destructor. */
        virtual ~Listener() {}
		
        //==============================================================================
        /** Called when the library file has changed, before it starts updating itself.
			Use this callback to initialise any data storage you may be holding.
		 */
        virtual void libraryChanged (ITunesLibrary *library) {};

        /** Called when the library has been updated.
		 */
        virtual void libraryUpdated (ITunesLibrary *library) = 0;

        /** Called when the library has finished updating.
		 */
        virtual void libraryFinished (ITunesLibrary *library) {}
    };
	
    /** Adds a listener to be called when this slider's value changes. */
    void addListener (Listener* listener);
	
    /** Removes a previously-registered listener. */
    void removeListener (Listener* listener);
	
    //==============================================================================
	
private:
	
	ListenerList <Listener> listeners;

    File libraryFile;
	ScopedPointer<ITunesLibraryParser> parser;
	ValueTree libraryTree;
};



#endif  // __ITUNESLIBRARY_H_CF4E7153__
